using Godot;
using System;
using System.Collections.Generic;

public partial class NCharacter : CharacterBody2D
{
	[ExportGroup("移动速度")]
	[Export] public float mMoveSpeed = 200;
	[ExportGroup("状态")]
	[Export] public CState mState;
	[ExportGroup("物品")]
	[Export] public Godot.Collections.Dictionary<int, CItem> mItems = new();
	[Export] public bool mLeftPressed = false;
	[Export] public bool mRightPressed = false;
	[Export] public bool mUpPressed = false;
	[Export] public bool mDownPressed = false;
	[Export] public DIR mDir;
	AnimatedSprite2D mAni;
	NInventory mInventory;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		mInventory = GetNodeOrNull<NInventory>("CanvasLayer/NInventory");
		mAni = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		mState?.run(this, delta);
	}

	public void doAni(ANI a)
	{
		mAni.Play(_aniName(a));
	}

	public override void _UnhandledInput(InputEvent @event)
	{
		if (@event is InputEventKey eventKey)
		{
			if (eventKey.Keycode == Key.A)
			{
				mLeftPressed = eventKey.Pressed;
			}
			if (eventKey.Keycode == Key.D)
			{
				mRightPressed = eventKey.Pressed;
			}
			if (eventKey.Keycode == Key.W)
			{
				mUpPressed = eventKey.Pressed;
			}
			if (eventKey.Keycode == Key.S)
			{
				mDownPressed = eventKey.Pressed;
			}
		}
	}

	public bool pickItem(CItem item)
	{
		return mInventory.pickItem(item);
	}

	public bool HasInput
	{
		get
		{
			return mLeftPressed || mRightPressed || mUpPressed || mDownPressed;
		}
	}

	string _aniName(ANI a)
	{
		return string.Format("{0}_{1}", a.ToString().ToLower(), mDir.ToString().ToLower());
	}
}
